package com.ruoyi.web.controller.biz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.ruoyi.biz.bean.bo.CaseInfoBO;
import com.ruoyi.biz.bean.vo.dto.CaseInfoDTO;
import com.ruoyi.biz.bean.vo.qo.CaseInfoQO;
import com.ruoyi.biz.domain.CaseInfo;
import com.ruoyi.biz.domain.CasePhase;
import com.ruoyi.biz.domain.CaseType;
import com.ruoyi.biz.domain.LitigateType;
import com.ruoyi.biz.service.caseinfo.ICaseInfoService;
import com.ruoyi.biz.service.casepahse.ICasePhaseService;
import com.ruoyi.biz.service.casetype.ICaseTypeService;
import com.ruoyi.biz.service.casetype.ILitigateTypeService;
import com.ruoyi.common.core.domain.CxSelect;
import com.ruoyi.common.core.page.PageQO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.interceptor.anno.MyPermissions;
import com.ruoyi.web.controller.core.impl.BaseBQCtrollerExImpls;

/**
 * 信息
 * 
 * @author riverbo
 */
@MyPermissions("案件信息")
@Controller
@RequestMapping("biz/caseinfo")
public class CaseInfoController
		extends BaseBQCtrollerExImpls<ICaseInfoService, CaseInfo, CaseInfoBO, CaseInfoQO, CaseInfoDTO> {

	@Resource
	private ICaseTypeService caseTypeService;

	@Resource
	private ILitigateTypeService litigateTypeService;

	@Resource
	private ICasePhaseService casePhaseService;
	
	@Override
	public String query(ModelMap mmap) throws Exception {
		mmap.put("canAdd", false);
		mmap.put("canEdit", false);
		mmap.put("canCheck", false);
		return super.query(mmap);
	}

	@Override
	public TableDataInfo query(CaseInfoQO q, PageQO page) throws Exception {
		return super.query(q, page);
	}

	@Override
	public String add(ModelMap mmap) throws Exception {
		List<CxSelect> lsCaseType = getCaseTypeData();
		mmap.put("ctData", lsCaseType);
		//
		List<CasePhase> lsCasePhase = casePhaseService.selectAll();
		mmap.put("casePhases", lsCasePhase);
		//
		CaseInfoBO vo = new CaseInfoBO();
		vo.setCaseTypeId(2);
		// vo.setLitigateTypeId(1);
		mmap.put("vo", vo);
		return super.add(mmap);
	}

	@Override
	public String edit(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
		List<CxSelect> lsCaseType = getCaseTypeData();
		mmap.put("ctData", lsCaseType);
		//
		List<CasePhase> lsCasePhase = casePhaseService.selectAll();
		mmap.put("casePhases", lsCasePhase);
		//
		return super.edit(id, mmap);
	}

	@Override
	public String check(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
		List<CxSelect> lsCaseType = getCaseTypeData();
		mmap.put("ctData", lsCaseType);
		//
		List<CasePhase> lsCasePhase = casePhaseService.selectAll();
		mmap.put("casePhases", lsCasePhase);
		//
		return super.check(id, mmap);
	}

	public String addx(ModelMap mmap) throws Exception {
		mmap.put("ctData", JSON.toJSON(getCaseTypeData()));
		CaseInfoBO vo = new CaseInfoBO();
		vo.setCaseTypeId(2);
		Map<String, String> map = new HashMap<>();
		map.put("caseTypeId", "1");
		map.put("caseTypeName", "诉吧");
		mmap.put("map", JSON.toJSON(map));
		// vo.setLitigateTypeId(1);
		mmap.put("vo", JSON.toJSON(vo));
		// mmap.put("data", JSON.toJSONString(getData()));
		return super.add(mmap);
	}

	protected List<CxSelect> getCaseTypeData() {
		List<CxSelect> cxList = new ArrayList<CxSelect>();
		{
			List<CaseType> lsCaseType = caseTypeService.selectAll();
			for (CaseType u : lsCaseType) {
				CxSelect cxSelectRoot = new CxSelect(String.valueOf(u.getCaseTypeId()), u.getCaseTypeName());
				List<CxSelect> subList = new ArrayList<CxSelect>();
				//
				List<LitigateType> lsLt = litigateTypeService.selectAll();
				for (LitigateType lt : lsLt) {
					if (u.getCaseTypeId().equals(2)) {
						if ( !lt.getLitigateTypeId().equals(4) ) {
							subList.add(new CxSelect(String.valueOf(lt.getLitigateTypeId()), lt.getLitigateTypeName()));
						}
					} else {
						subList.add(new CxSelect(String.valueOf(lt.getLitigateTypeId()), lt.getLitigateTypeName()));
					}
				}
				cxSelectRoot.setS(subList);
				//
				cxList.add(cxSelectRoot);
			}
		}
		return cxList;
	}

	protected List<CxSelect> getData() {
		List<CxSelect> cxList = new ArrayList<CxSelect>();
		{
			CxSelect cxSelectRoot = new CxSelect("淘宝", "taobao");
			//
			List<CxSelect> subList = new ArrayList<CxSelect>();
			subList.add(new CxSelect("天猫", "tm"));
			subList.add(new CxSelect("聚划算", "jhs"));
			//
			cxSelectRoot.setS(subList);
			//
			cxList.add(cxSelectRoot);
		}
		{
			CxSelect cxSelectRoot = new CxSelect("京东", "jd");
			//
//	        List<CxSelect> subList = new ArrayList<CxSelect>();
//	        subList.add(new CxSelect("京东超市", "jdcs"));
//	        subList.add(new CxSelect("京东生鲜", "jdsx"));
//	        //
//	        cxSelectRoot.setS(subList);
			//
			cxList.add(cxSelectRoot);
		}
		{
			CxSelect cxSelectRoot = new CxSelect("抖音", "ty");
			cxList.add(cxSelectRoot);
		}

		return cxList;
	}
	
}